使用Python進行網路自動化編程時,會使用到許多的外掛模組及框架,當許多東西安裝在一起時就可能發生模組衝突。這篇文章說明如何使用Virtualenv建立獨立的python專案開發環境及使用pip管理模組套件。
$ sudo apt install python-virtualenv
$ virtualenv --version
virtualenv 20.13.0+ds from /usr/lib/python3/dist-packages/virtualenv/__init__.py
$ sudo apt install python-pip3
$ pip --version
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
$ pip list | wc -l
105
$ mkdir my-first-env
$ virtualenv my-first-env -p python3.10
created virtual environment CPython3.10.6.final.0-64 in 110ms
creator CPython3Posix(dest=/home/pchan/GitHub/my-first-env, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/pchan/.local/share/virtualenv)
added seed packages: pip==22.3.1, setuptools==65.6.3, wheel==0.38.4
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
$ source my-first-env/bin/activate
(my-first-env) $
(my-first-env) $ pip list
Package Version
---------- -------
pip 22.3.1
setuptools 65.6.3
wheel 0.38.4
(my-first-env) pchan@LAPTOP-UP9VD31I:~/GitHub$ pip install paramiko
Collecting paramiko
Using cached paramiko-2.12.0-py2.py3-none-any.whl (213 kB)
Collecting pynacl>=1.0.1
Using cached PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (856 kB)
Collecting bcrypt>=3.1.3
Using cached bcrypt-4.0.1-cp36-abi3-manylinux_2_28_x86_64.whl (593 kB)
Collecting six
Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting cryptography>=2.5
Using cached cryptography-38.0.4-cp36-abi3-manylinux_2_28_x86_64.whl (4.2 MB)
Collecting cffi>=1.12
Using cached cffi-1.15.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (441 kB)
Collecting pycparser
Using cached pycparser-2.21-py2.py3-none-any.whl (118 kB)
Installing collected packages: six, pycparser, bcrypt, cffi, pynacl, cryptography, paramiko
Successfully installed bcrypt-4.0.1 cffi-1.15.1 cryptography-38.0.4 paramiko-2.12.0 pycparser-2.21 pynacl-1.5.0 six-1.16.0
(my-first-env) pchan@LAPTOP-UP9VD31I:~/GitHub$ pip list
Package Version
------------ -------
bcrypt 4.0.1
cffi 1.15.1
cryptography 38.0.4
paramiko 2.12.0 (paramiko已安裝,其餘所需的套件也一并安裝了)
pip 22.3.1
pycparser 2.21
PyNaCl 1.5.0
setuptools 65.6.3
six 1.16.0
wheel 0.38.4
(my-first-env) pchan@LAPTOP-UP9VD31I:~/GitHub$ pip install netmiko==3.4.0
Collecting netmiko==3.4.0
Downloading netmiko-3.4.0-py3-none-any.whl (178 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 178.9/178.9 kB 258.8 kB/s eta 0:00:00
Collecting ntc-templates
Using cached ntc_templates-3.2.0-py3-none-any.whl (354 kB)
Collecting tenacity
Using cached tenacity-8.1.0-py3-none-any.whl (23 kB)
Collecting scp>=0.13.2
Using cached scp-0.14.4-py2.py3-none-any.whl (8.6 kB)
Requirement already satisfied: paramiko>=2.6.0 in ./my-first-env/lib/python3.10/site-packages (from netmiko==3.4.0) (2.12.0)
Requirement already satisfied: setuptools>=38.4.0 in ./my-first-env/lib/python3.10/site-packages (from netmiko==3.4.0) (65.6.3)
Collecting pyserial
Using cached pyserial-3.5-py2.py3-none-any.whl (90 kB)
Requirement already satisfied: bcrypt>=3.1.3 in ./my-first-env/lib/python3.10/site-packages (from paramiko>=2.6.0->netmiko==3.4.0) (4.0.1)
Requirement already satisfied: six in ./my-first-env/lib/python3.10/site-packages (from paramiko>=2.6.0->netmiko==3.4.0) (1.16.0)
Requirement already satisfied: cryptography>=2.5 in ./my-first-env/lib/python3.10/site-packages (from paramiko>=2.6.0->netmiko==3.4.0) (38.0.4)
Requirement already satisfied: pynacl>=1.0.1 in ./my-first-env/lib/python3.10/site-packages (from paramiko>=2.6.0->netmiko==3.4.0) (1.5.0)
Collecting textfsm<2.0.0,>=1.1.0
Downloading textfsm-1.1.3-py2.py3-none-any.whl (44 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.7/44.7 kB 1.7 MB/s eta 0:00:00
Requirement already satisfied: cffi>=1.12 in ./my-first-env/lib/python3.10/site-packages (from cryptography>=2.5->paramiko>=2.6.0->netmiko==3.4.0) (1.15.1)
Collecting future
Using cached future-0.18.2-py3-none-any.whl
Requirement already satisfied: pycparser in ./my-first-env/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=2.5->paramiko>=2.6.0->netmiko==3.4.0) (2.21)
Installing collected packages: pyserial, tenacity, future, textfsm, ntc-templates, scp, netmiko
Successfully installed future-0.18.2 netmiko-3.4.0 ntc-templates-3.2.0 pyserial-3.5 scp-0.14.4 tenacity-8.1.0 textfsm-1.1.3
(my-first-env) pchan@LAPTOP-UP9VD31I:~/GitHub$ pip install --upgrade netmiko
Requirement already satisfied: netmiko in ./my-first-env/lib/python3.10/site-packages (3.4.0)
Collecting netmiko
Using cached netmiko-4.1.2-py3-none-any.whl (196 kB)
Requirement already satisfied: setuptools>=38.4.0 in ./my-first-env/lib/python3.10/site-packages (from netmiko) (65.6.3)
Requirement already satisfied: ntc-templates>=2.0.0 in ./my-first-env/lib/python3.10/site-packages (from netmiko) (3.2.0)
Requirement already satisfied: paramiko>=2.7.2 in ./my-first-env/lib/python3.10/site-packages (from netmiko) (2.12.0)
Requirement already satisfied: tenacity in ./my-first-env/lib/python3.10/site-packages (from netmiko) (8.1.0)
Requirement already satisfied: scp>=0.13.3 in ./my-first-env/lib/python3.10/site-packages (from netmiko) (0.14.4)
Requirement already satisfied: pyserial in ./my-first-env/lib/python3.10/site-packages (from netmiko) (3.5)
Collecting pyyaml>=5.3
Using cached PyYAML-6.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (682 kB)
Collecting textfsm==1.1.2
Using cached textfsm-1.1.2-py2.py3-none-any.whl (44 kB)
Requirement already satisfied: future in ./my-first-env/lib/python3.10/site-packages (from textfsm==1.1.2->netmiko) (0.18.2)
Requirement already satisfied: six in ./my-first-env/lib/python3.10/site-packages (from textfsm==1.1.2->netmiko) (1.16.0)
Requirement already satisfied: bcrypt>=3.1.3 in ./my-first-env/lib/python3.10/site-packages (from paramiko>=2.7.2->netmiko) (4.0.1)
Requirement already satisfied: pynacl>=1.0.1 in ./my-first-env/lib/python3.10/site-packages (from paramiko>=2.7.2->netmiko) (1.5.0)
Requirement already satisfied: cryptography>=2.5 in ./my-first-env/lib/python3.10/site-packages (from paramiko>=2.7.2->netmiko) (38.0.4)
Requirement already satisfied: cffi>=1.12 in ./my-first-env/lib/python3.10/site-packages (from cryptography>=2.5->paramiko>=2.7.2->netmiko) (1.15.1)
Requirement already satisfied: pycparser in ./my-first-env/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=2.5->paramiko>=2.7.2->netmiko) (2.21)
Installing collected packages: textfsm, pyyaml, netmiko
Attempting uninstall: textfsm
Found existing installation: textfsm 1.1.3
Uninstalling textfsm-1.1.3:
Successfully uninstalled textfsm-1.1.3
Attempting uninstall: netmiko
Found existing installation: netmiko 3.4.0
Uninstalling netmiko-3.4.0:
Successfully uninstalled netmiko-3.4.0
Successfully installed netmiko-4.1.2 pyyaml-6.0 textfsm-1.1.2
(my-first-env) pchan@LAPTOP-UP9VD31I:~/GitHub$ pip freeze > requirement.txt (建立安裝清單)
(my-first-env) pchan@LAPTOP-UP9VD31I:~/GitHub$ cat requirement.txt (檢視模組清單)
bcrypt==4.0.1
cffi==1.15.1
cryptography==38.0.4
future==0.18.2
netmiko==4.1.2
ntc-templates==3.2.0
paramiko==2.12.0
pycparser==2.21
PyNaCl==1.5.0
pyserial==3.5
PyYAML==6.0
scp==0.14.4
six==1.16.0
tenacity==8.1.0
textfsm==1.1.2
(my-first-env) pchan@LAPTOP-UP9VD31I:~/GitHub$ deactivate (離開my-first-env)
pchan@LAPTOP-UP9VD31I:~/GitHub$ mkdir my-second-env (建立資料夾)
pchan@LAPTOP-UP9VD31I:~/GitHub$ virtualenv my-second-env -p python3.10 (建立第二個env)
pchan@LAPTOP-UP9VD31I:~/GitHub$ source my-second-env/bin/activate
(my-second-env) pchan@LAPTOP-UP9VD31I:~/GitHub$ pip install -r requirement.txt
(my-second-env) pchan@LAPTOP-UP9VD31I:~/GitHub$ pip list
Package Version
------------- -------
bcrypt 4.0.1
cffi 1.15.1
cryptography 38.0.4
future 0.18.2
netmiko 4.1.2
ntc-templates 3.2.0
paramiko 2.12.0
pip 22.3.1
pycparser 2.21
PyNaCl 1.5.0
pyserial 3.5
PyYAML 6.0
scp 0.14.4
setuptools 65.6.3
six 1.16.0
tenacity 8.1.0
textfsm 1.1.2
wheel 0.38.